<template>
  <div class="article-list">
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh" success-text="刷新成功">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <article-item
          v-for="article in articles"
          :key="article.atr_id"
          :article="article"
          @dislike="dislike"
        ></article-item>
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script>
import to from 'await-to-js'
import { articles } from '@/api/article'
import ArticleItem from '@/components/ArticleItem.vue'
export default {
  name: 'ArticleList',
  components: {
    ArticleItem
  },
  data () {
    return {
      articles: [],
      loading: false,
      finished: false,
      refreshing: false,
      time: +new Date(),
      oldTime: 1552266004800
    }
  },
  props: {
    ChannelsId: {
      type: Number,
      required: true
    }
  },
  async created () {
  },
  methods: {
    async onLoad () {
      const netxPage = await this.getArticles(this.oldTime)
      if (netxPage.length === 0) {
        this.finished = true
        return
      }
      this.oldTime -= 365
      this.articles.push(...netxPage)
      this.loading = false
    },
    async onRefresh () {
      this.articles = await this.getArticles(this.time)
      this.refreshing = false
    },
    async getArticles (time) {
      const [, result] = await to(
        articles({
          channel_id: this.ChannelsId,
          timestamp: time
        })
      )
      return result.data.data.results
    },
    dislike (id) {
      this.articles = this.articles.filter(item => item.art_id !== id)
    }
  }
}
</script>
<style scoped lang="less">
.article-list {
  height: 100%;
  overflow-y: auto;
  padding: 0 16px 50px;
}
</style>
